package com.gz.servlet.httpservletrequest;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/checkServlet")
public class CheckServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("pwd");
        if("tom".equals(username)&&"123".equals(password)){
            request.setAttribute("role", "管理员");
        }else{
            request.setAttribute("role", "普通员工");
        }

        //"/mangerServlet"写的是要转发的Servlet的URL
        // /会被解析成http://localhost:8080/servlet
        //只能转发本web内的网站，如百度之类的不能转发只能重定向
        //"转发"和"重定向"之间的区别:
        //1.RequestDispatcher转发是在服务器端完成的，服务器端有http://localhost:8080/servlet路径
        //所以只要写后面要转发的servleturl-pattern即可
        //2.而重定向response.sendRedirect()是在浏览器(客户端)完成，浏览器只有http://localhost:8080/
        //所以用response.sendRedirect()时，路径必须写全，指定web路径
        //写成/servlet//mangerServlet才行
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/mangerServlet");
        //将request和response给下一个servlet
        requestDispatcher.forward(request, response);
    }
}
